<?php

class AmpJqgridHelper extends Helper
{

  public function grid($id, $route, $cols, $params = null)
  {
    $params = array();
    
    $url = $this->response->makeUrl($route);
    
    $params = ParameterHolder::parseFrom($params);
    
    $colNames = array();
    $colModel = array();
    
    foreach($cols as $name=>$col) {
      $colNames[] = $name;
      $colModel[] = $col;
    }
    
    $params->setDefault('url', "'$url'");
    $params->setDefault('datatype', "'json'");
    $params->setDefault('colNames', json_encode($colNames));
    $params->setDefault('colModel', json_encode($colModel));
    $params->setDefault('rowNum', "10");
    $params->setDefault('mtype', "'POST'");
    $params->setDefault('pager', "'#users-pager'");
    $params->setDefault('viewrecords', "true");
    $params->setDefault('sortname', "'id'");
    $params->setDefault('sortorder', "'desc'");
    $params->setDefault('jsonReader', "{ repeatitems: false, id: 'id'}");
    $params->setDefault('autowidth', "true");
    $params->setDefault('multiselect', "true");
    $params->setDefault('height', '"100%"');
    
    $js = "$(function() { $('#$id-table').jqGrid({\n";
    
    $_params = array();
    $params = $params->getAll();
    foreach($params as $name=>$value) {
      $_params[] = $name.': '.$value;
    }
    $js .= implode(",\n ", $_params);
    
    $js .= "});";
    
    $js .= "jQuery('#$id-table')
      .navGrid('#$id-pager',{edit:false,add:false,del:false,search:false})
      .navButtonAdd('#$id-pager',{
         caption:'".__('Edit')."', 
         buttonicon:'ui-icon-add', 
         onClickButton: $.Amp.jqGrid.Edit,
         position:'last'
      })
      .navButtonAdd('#$id-pager',{
         caption:'".__('Delete')."', 
         buttonicon:'ui-icon-del', 
         onClickButton: $.Amp.jqGrid.Delete,
         position:'last'
      }); });";
    
    return $this->javascript->block($js);
  }

}

?>